import { Component, OnInit, Input, OnChanges, SimpleChanges } from '@angular/core';
import { TagData } from "../../../../entity/tag-data.model";
import { SubStateVOList } from "../limited-turbine";
import { TurboperCategory } from "../../../../entity/turboper-category.model";
import { TurboperHistory } from ".././../../../entity/turboper-history.model";
import { TurboperHistoryService } from "../../../../service/turboper-history.service";
@Component({
  selector: 'app-turbine',
  templateUrl: './turbine.component.html',
  styleUrls: ['./turbine.component.css']
})
export class TurbineComponent implements OnInit {

  /**
   * 风机状态颜色
   * 
   * @type {string}
   * @memberof TurbineComponent
   */
  @Input()
  statusColor: string;
  /**
   * 风机子状态
   * 
   * @type {number}
   * @memberof TurbineComponent
   */
  @Input()
  turbSubState: number;
  /**
   * 是否显示子状态详细信息
   * 
   * @type {boolean}
   * @memberof TurbineComponent
   */
  @Input()
  isShowCategory: boolean;
  /**
   * 平均风速
   * 
   * @type {string}
   * @memberof TurbineComponent
   */

  @Input()
  windSpeed: number;

  /**
   * 功率
   * 
   * @type {string}
   * @memberof TurbineComponent
   */
  @Input()
  power: number;

  /**
   * 风机名
   * 
   * @type {string}
   * @memberof TurbineComponent
   */
  @Input()
  turbineName: string;
  /**
  * 风机id
  * 
  * @type {number}
  * @memberof TurbineComponent
  */
  @Input()
  turbineId: number;
  /**
   * 
   * 
   * @type {TagData}
   * @memberof TurbineComponent
   */
  @Input()
  turbineStatus: TagData;
  @Input()
  operCategorys: TurboperCategory[];
  @Input()
  turBenchMark: boolean;
  @Input()
  showMainTurb: boolean;
  currentTurbSub: TurboperCategory;
  isRun: boolean = false;
  isMouseEnter: boolean = false;
  constructor(
    private turbHistSer: TurboperHistoryService
  ) {
  }
  ngOnChanges(changes: SimpleChanges): void {
    if (changes['turbSubState'] && changes['turbSubState'].currentValue) {

      if (this.operCategorys) {
        let index = this.operCategorys.findIndex(o => o.operCategoryCode == this.turbSubState.toString());
        if (index > -1) {
          this.currentTurbSub = this.operCategorys[index];
        }
      }

    }
    if (changes['operCategorys'] && changes['operCategorys'].currentValue) {
      if (this.turbSubState) {
        const index = this.operCategorys.findIndex(o => o.operCategoryCode == this.turbSubState.toString());
        if (index > -1) {
          this.currentTurbSub = this.operCategorys[index];
        }
      }

    }
  }
  ngOnInit() {
  }

  initData() {
    if (this.turbineStatus.value == 1 || this.turbineStatus.value ==6) {
      this.isRun = true;
    } else {
      this.isRun = false;
    }
  }
  triangleRight: boolean = false;
  triangleBottom: boolean = false;
  currentTurbHist: TurboperHistory;
  timer: any;
  onMouseEnter(ev) {
    clearTimeout(this.timer);
    this.timer = setTimeout(() => {
      this.getCurrentOperInfo(ev)
    }, 300)
  }
  getCurrentOperInfo(ev: any) {
    let l = ev.clientX - ev.offsetX,
      t = ev.clientY - ev.offsetY,
      clientWidth = document.body.clientWidth,
      clientHeight = document.body.clientHeight;
    if ((l + 300 + 95) > clientWidth) {
      this.triangleRight = true;
    } else {
      this.triangleRight = false;
    }
    if ((t + 280) > clientHeight) {
      this.triangleBottom = true;
    } else {
      this.triangleBottom = false;
    }
    ev.returnValue = false;
    //this.isMouseEnter = true;
    this.turbHistSer.getCurrentOperInfo(this.turbineId, this.currentTurbSub.operCategoryId)
      .then(res => {

        if (!res) return false;
        this.currentTurbHist = res;
        this.isMouseEnter = true;
      })
    return false;
  }
  //鼠标移入移出事件
  onMouseLeave(event) {
    clearTimeout(this.timer);
    this.isMouseEnter = false;
  }
}




// WEBPACK FOOTER //
// ./src/app/module/monitor/turbine-list/turbine/turbine.component.ts